<?php
require_once("test_7_common.php");
if(!$user){
    header("localtion:login.php");
}

$requestMethod = $_SERVER["REQUEST_METHOD"];
if($requestMethod=="GET"){
    $id = getParam("Id");
    $sql = "SELECT * FROM article WHERE Id = $id";
    $conn = createDb();
    $rs = $conn->query($sql);
    if($conn->error){
        die($conn->error);
    }
    $detail = $rs->fetch_assoc();
    if($detail["AuthorID"]!==$user["Id"]){
        header("localtion:login.php");
    }
}else{
    $id = getParam("id");
    $title = $_POST["Title"];
    $content = $_POST["Content"];
    $user = $_SESSION["user"];
    $updateTime = date_format(new DateTime(),"Y-m-d H:i:s");
    $sql ="UPDATE article SET Title='$title',Content='$content',UpdateTime='$updateTime' WHERE Id='$id' AND AuthorID=".$user["Id"];
    $conn = createDb();
    $rs = $conn -> query($sql);
    if($conn->error){
        echo $conn->error;
        die($conn->error);
    }
    Header("location:test_7_article_list.php");
    die("OK");
}
require_once("test_7_header.php");
?>
        <form method="POST" action="test_7_article_modify.php?id=<?=$id ?>" class="dataset">
            <div class="field">
                <label>标题</label>
                <input type="text" name="Title" value="<?= $detail["Title"]?>" />
            </div>
            <div class="field">
                <label>内容</label>
                <textarea name="Content"><?php echo $detail["Content"]?></textarea>
            </div>
            <div class="info">
                <?=$detail["AuthorName"]?> 编辑于
                <?=$detail["UpdateTime"]?>
            </div>
            <div class="actions">
                <input type="submit" value="提交" />
                <a href="test_7_article_list.php">返回列表</a>
            </div>
        </form>
<?php require_once("test_7_footer.php");?>